package com.thoughtservice.jspdoc.processors;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.thoughtservice.jspdoc.JSPFileList;
import com.thoughtservice.jspdoc.JSPPage;
import com.thoughtservice.jspdoc.RE;
import com.thoughtservice.jspdoc.RESyntaxException;
import com.thoughtservice.jspdoc.utils.ProcessorUtils;

public class ProcessLinksTo implements DocProcessor{

	private final static Logger LOG = LoggerFactory.getLogger(ProcessLinksTo.class);
		
	public boolean process(JSPPage p, String pc, JSPFileList fileList)
			throws RESyntaxException {
		LOG.trace("Entering {}", "ProcessLinksTo");
		RE re;
		//		re = new RE("<a[:space:]*href=\".*\"[:space:]*>([:print:]*</a>){1}");
		//		re = new RE("<a[:space:]*href=\"[^\"]*\"[:space:]*>[^</a>]*</a>");
		re = new RE("<a[:space:]*href=\"[^\"<]*");
		int index = 0;
		while (re.match(pc, index)) {
			//			System.out.println(re.getParen(0));
			String link = re.getParen(0);
			index = re.getParenEnd(0); 
//System.out.println("hello");				
			// Don't process mailto links and javascript:void's or javascript:functions
			if (link.indexOf("\"mailto:") == -1 && link.indexOf("javascript:") == -1) {
				link = ProcessorUtils.figureLink(link, p, JSPPage.LINKSTO);
				if (link != null) {
					JSPPage dest = ProcessorUtils.getPage(link, fileList);
					if (dest != null)
						dest.addConnection(JSPPage.LINKSFROM, p.getName());
				}
			}
		}
		LOG.trace("Exiting {}", "ProcessLinksTo");
	
		return false;
	}
}
